//
//  WBTitleButton.swift
//  微博
//
//  Created by 康智桐 on 2018/4/22.
//  Copyright © 2018年 Kk. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {
    //重载构造函数
    //title 如果是nil 显示首页
    //如果不为nil ，显示title和箭头
    
    init(title: String?) {
        super.init(frame:CGRect())
        
        //判断title是否为nil
        if title == nil {
            setTitle("首页", for: [])
        }else {
            setTitle(title! + " ", for: [])
            
            //设置图像
            setImage(UIImage(named:"navigationbar_arrow_down"), for: [])
            setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)

        }
        
    //设置字体和颜色
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        
        
        //设置大小
        sizeToFit()
        
            }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重新布局子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //判断label 和 imageview是否同时存在
        guard let titleLabel = titleLabel,
        let imageView = imageView
        else {
            return
        }
        //将label的x向左移动imageview的 宽度
        titleEdgeInsets = UIEdgeInsetsMake(0, -(imageView.bounds.width), 0, imageView.bounds.width)

        
        //将imageview 的x 向右移动 label的宽度
        imageEdgeInsets = UIEdgeInsetsMake(0, titleLabel.bounds.width, 0, -(titleLabel.bounds.width))
        
        
        
    }
    
    
}
